package com.lxmes.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxmes.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.base.domain.JcWarehouseinfo;
import com.lxmes.base.service.IJcWarehouseinfoService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 仓库管理Controller
 * 
 * @author 王超
 * @date 2022-06-11
 */
@RestController
@RequestMapping("/base/warehouseinfo")
public class JcWarehouseinfoController extends BaseController
{
    @Autowired
    private IJcWarehouseinfoService jcWarehouseinfoService;

    /**
     * 查询仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcWarehouseinfo jcWarehouseinfo)
    {
        startPage();
        List<JcWarehouseinfo> list = jcWarehouseinfoService.selectJcWarehouseinfoList(jcWarehouseinfo);
        return getDataTable(list);
    }

    @GetMapping("/getListWarehouseinfo")
    public AjaxResult getListWarehouseinfo()
    {
        JcWarehouseinfo jcWarehouseinfo=new JcWarehouseinfo();
        List<JcWarehouseinfo> list = jcWarehouseinfoService.selectJcWarehouseinfoList(jcWarehouseinfo);
        JSONArray jsonArray=new JSONArray();
        for (int i = 0; i <list.size() ; i++) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("value",list.get(i).getStockCode());
            jsonObject.put("label",list.get(i).getStockName());
            jsonArray.add(jsonObject);
        }
        return AjaxResult.success(jsonArray);
    }


    /**
     * 导出仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfo:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcWarehouseinfo jcWarehouseinfo)
    {
        jcWarehouseinfo.setDeptId(SecurityUtils.getDeptId());
        List<JcWarehouseinfo> list = jcWarehouseinfoService.selectJcWarehouseinfoList(jcWarehouseinfo);
        ExcelUtil<JcWarehouseinfo> util = new ExcelUtil<JcWarehouseinfo>(JcWarehouseinfo.class);
        util.exportExcel(response, list, "仓库管理数据");
    }

    /**
     * 获取仓库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jcWarehouseinfoService.selectJcWarehouseinfoById(id));
    }
    @GetMapping(value = "/getWarehouseInfoByDeptId/{deptId}")
    public AjaxResult getWarehouseInfoByDeptId(@PathVariable("deptId") String deptId)
    {
        return AjaxResult.success(jcWarehouseinfoService.selectJcWarehouseinfoDeptId(deptId));
    }

    /**
     * 新增仓库管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfo:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcWarehouseinfo jcWarehouseinfo)
    {

        JcWarehouseinfo searchWare = new JcWarehouseinfo();
        searchWare.setStockName(jcWarehouseinfo.getStockName());

        List<JcWarehouseinfo> jcWarehouseinfoList = jcWarehouseinfoService.selectJcWarehouseinfoList(searchWare);

        if(jcWarehouseinfoList!=null&&jcWarehouseinfoList.size()>0){
            return AjaxResult.error("新增仓库'" + jcWarehouseinfo.getStockName() + "'失败，仓库名称已存在");
        }

        jcWarehouseinfo.setDeptId(SecurityUtils.getDeptId());
        return toAjax(jcWarehouseinfoService.insertJcWarehouseinfo(jcWarehouseinfo));
    }

    /**
     * 修改仓库管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfo:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcWarehouseinfo jcWarehouseinfo)
    {
        return toAjax(jcWarehouseinfoService.updateJcWarehouseinfo(jcWarehouseinfo));
    }

    /**
     * 删除仓库管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouseinfo:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jcWarehouseinfoService.deleteJcWarehouseinfoByIds(ids));
    }
}
